<template>
	<view v-show="show" class="form-container">
		<view class="form-area" :style="{ height: `${formHeight}px` }">
			<view class="nav-area">
				<view
					style="display: flex;flex-direction: row;background-color: #FFFFFF;padding-bottom: 30rpx;align-items: center;">
					<view style="margin-left: 30rpx;" @click="closeWrapper">预览</view>
					<view style="margin-left: 200rpx;font-size: 36rpx;">{{ currentFormItem.title}}编辑</view>
				</view>
				<view style="overflow-y: scroll;width: 690rpx;margin: 30rpx;flex: 1;" >
					<view v-for="item in currentEditableForm" :key="item.id" @click="handleClickTitle(item)">
						<view style="font-size: 36rpx;font-weight: bold;margin-bottom: 10rpx;">{{ item.title }}</view>
						<view :class="{'area-can-edit' : item.type != 'text'}" v-html="item.content"></view>
					</view>
				</view>
			</view>
			<view class="edit-area">
				<view class="edit-component">
					<template v-if="currentFormItem.type === 'input'">
						<input v-model="currentValue" />
					</template>
					<template v-if="currentFormItem.type === 'rich-text'">
						<HulaEditor :editorId="`${currentFormItem.id}-form-editor`" :key="`${currentFormItem.id}-form-editor`"
							v-model="currentValue" :height="currentFormItem.props.height" />
					</template>
					<template v-if="currentFormItem.type === 'textarea'">
						<textarea :show-confirm-bar="false" :style="{'height': currentFormItem.props.height+80 }"
							style="background-color: #FFFFFF;width: 710rpx;padding: 20rpx;"
							placeholder-style="font-style:italic" placeholder="请输入..."
							v-model="currentValue"></textarea>
					</template>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import HulaEditor from './editor'
	import component from '../components.js'
	export default {
		mixins: [component],
		components: {
			HulaEditor
		},
		props: {
			form: {
				type: Array,
				default: [{
						id: '111',
						title: 'your name',
						type: 'input',
						editable: true,
						props: {},
					},
					{
						id: '222',
						title: 'your description',
						type: 'rich-text',
						editable: true,
						props: {
							height: 280,
							value: '<h3>title</h3>',
						},
					},
				],
			},
			show: {
				type: Boolean,
				default: false,
			},
			defaultId: {
				type: String,
				default: '',
			},
		},
		data() {
			return {
				formHeight: 220,
				keyHei: 0,
				currentForm: [],
				currentFormItem: {},
				currentValue: undefined,
				originHeight: 0,
			}
		},
		mounted() {
			uni.onKeyboardHeightChange((res) => {
				this.keyHei = res.height
			})
			
			// #ifdef H5
			if (this.sysInfo.osName.includes('ios')) {
				// ios webview 会滚动
				document.getElementsByClassName('form-container')[0].addEventListener('scroll', (e) => { 
					if (!this.keyHei) {
						this.keyHei = document.getElementsByClassName('form-container')[0].scrollTop - 24
					}
				}, false);
			} else {
				// android webview 会被挤压，可以监听 window resize 判断键盘弹出
				this.originHeight = document.documentElement.clientHeight || document.body.clientHeight
				window.addEventListener('resize', () => { 
					const resizeHeight = document.documentElement.clientHeight || document.body.clientHeight
					if (resizeHeight < this.originHeight) { 
						this.keyHei = this.originHeight - resizeHeight - 24
					}
				}, false);
			}
			// #endif
			
			const currentForm = JSON.parse(JSON.stringify(this.form))
			this.currentForm = currentForm
			if (this.defaultId !== '') {
				const index = this.form.findIndex((item) => item.id === this.defaultId)
				if (index >= 0) {
					this.currentFormItem = currentForm[index]
					this.currentValue = currentForm[index].props.value
				}
			}
		},
		unmounted() {
			// #ifdef H5
			if (this.sysInfo.osName.includes('ios')) {
				window.removeEventListener('scroll')
			} else {
				window.removeEventListener('resize')
			}
			// #endif
		},
		methods: {
			handleClickTitle(item) {
				if (!this.currentFormItem.id) {
					this.currentFormItem = item
					this.currentValue = item.props.value
				} else if (item.id !== this.currentFormItem.id) {
					const index = this.currentForm.findIndex(
						(i) => i.id === this.currentFormItem.id
					)
					if (index >= 0) {
						this.currentForm[index].props.value = this.currentValue
						this.currentFormItem = item
						this.currentValue = item.props.value
						
					}
				}
			},
			closeWrapper() {
				const index = this.currentForm.findIndex(
					(i) => i.id === this.currentFormItem.id
				)
				if (index >= 0) {
					this.currentForm[index].props.value = this.currentValue
				}
				this.$emit('closeWrapper', JSON.parse(JSON.stringify(this.currentForm)))
			},
		},
		computed: {
			currentEditableForm() {
				return this.currentForm.filter(
					(item) => typeof item.editable === 'undefined' || item.editable
				)
			},
		},
		watch: {
			keyHei(val, old) {
				if (val) {
					console.log('formHeight change!', this.windowHeight - val)
					// 键盘唤起，控制 bottom 紧贴
					this.formHeight = this.windowHeight - val - 44
				}
			},
			form(val) {
				const currentForm = JSON.parse(JSON.stringify(val))
				this.currentForm = currentForm
			},
			defaultId(val) {
				if (val !== '') {
					const index = this.currentForm.findIndex(
						(item) => item.id === this.defaultId
					)
					if (index >= 0) {
						this.currentFormItem = this.currentForm[index]
						this.currentValue = this.currentForm[index].props.value
					}
				}
			},
		},
	}
</script>

<style lang="scss" scoped>
	.form-container {
		position: fixed;
		top: 44px;
		
		// #ifdef H5
		top: 40rpx;
		// #endif
		
		left: 0;
		right: 0;
		bottom: 0;
		background: rgba(0, 0, 0, 0.4);
		transition: 0.6s;
		z-index: 10;

		.form-area {
			position: absolute;
			left: 0;
			right: 0;
			top: 0;
			display: flex;
			flex-direction: column;
			transition: 0.6s;

			.nav-area {
				flex: 1;
				background: #eee;
				min-height: 280rpx;
				display: flex;
				flex-direction: column;

				.area-can-edit {
					padding: 20rpx;
					background-color: #FFFFFF;
					border-radius: 10rpx;
				}
			}

			.edit-area {
				height: fit-content;
				flex: 0;
			}
		}
	}
</style>
